package test;

import test.bean.UserBean;

import java.awt.EventQueue;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.event.*;

public class wareHouse<game> {

    JFrame frame;
    private JTextField textField;
    private UserBean user;

    private static final String string01 = "PUBG";
    private static final String string02 = "NBA 2K";
    private static final String string03 = "PICO PARK";
    private static final String string04 = "永劫无间";
    private static final String game[] = {string01, string02, string03, string04};
    private static final String string_01 = "《绝地求生》(PUBG) 是由韩国Krafton工作室开发的一款战术竞技型射击类沙盒游戏 [1]  [36-37]  。" +
            "在该游戏中，玩家需要在游戏地图上收集各种资源，并在不断缩小的安全区域内对抗其他玩家，让自己生存到最后 [2]  。" +
            "游戏《绝地求生》除获得G-STAR最高奖项总统奖以及其他五项大奖 [3]  ，还打破了7项吉尼斯纪录。";
    private static final String string_02 = "《NBA 2K》是一款由Take-Two Interactive旗下的Rockstar Games、2K Games制作的NBA篮球类游戏，初版发行于1999年11月10日。z" +
            "此系列作品有着全方位的NBA体验、精致的球员人工智能和招牌动作等特色，用户可以通过细致精确的操作来感受篮球运动的精妙之处，并体会篮球战术打法的美丽。";
    private static final String string_03 = "《PICO PARK:Classic Edition》是一款非常好玩的多人合作动作益智游戏，将会带个玩家们一起合作过关带来的乐趣。在所有的48个关卡中玩家将会体验专门为多人合作而设计的谜题。";
    private static final String string_04 = "《永劫无间》是由网易旗下“24 Entertainment工作室”开发的一款多人动作竞技游戏，于2021年7月8日开启不删档测试，《永劫无间》Steam全球公测于2021年8月12日10：00正式开服，同时国服与Steam平台同步上线S1浪潮赛季。EPIC平台上线时间将延期至9月中旬。 " +
            "游戏以虚构的幻想世界为背景，阴阳二神互相杀伐，导致世界处于永劫之中。玩家需扮演一名英雄，在地图中收集各种资源，与其他玩家对抗，生存到最后。";
    private static final String game_information[] = {string_01, string_02, string_03, string_04};
    private static final String game_problem[] = {"网络卡顿", "闪退", "无法更新", "素材显示不完全", "其他"};

    private static final String path = "image/";
    private static final String pubg1 = "pubg01.png";
    private static final String pubg2 = "pubg02.png";
    private static final String pubg3 = "pubg03.png";
    private static final String pubg_image[] = {path + pubg1, path + pubg2, path + pubg3};

    private static final String nba_2k1 = "nba_2k01.png";
    private static final String nba_2k2 = "nba_2k02.png";
    private static final String nba_2k3 = "nba_2k03.png";
    private static final String nba_2k_image[] = {path + nba_2k1, path + nba_2k2, path + nba_2k3};

    private static final String pico01 = "pico01.png";
    private static final String pico02 = "pico02.png";
    private static final String pico03 = "pico03.png";
    private static final String pico_image[] = {path + pico01, path + pico02, path + pico03};

    private static final String yongJie01 = "yongjie01.png";
    private static final String yongJie02 = "yongjie02.png";
    private static final String yongJie03 = "yongjie03.png";
    private static final String yongJie_image[] = {path + yongJie01, path + yongJie02, path + yongJie03};

    /**
     * Launch the application.
     */
//    public static void main(String[] args) {
//        EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                try {
//                    wareHouse window = new wareHouse();
//                    window.frame.setVisible(true);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    /**
     * Create the application.
     */
    public wareHouse() {
        initialize();
    }

    public wareHouse(UserBean user) {
        this.user = user;
        initialize();
    }


    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 30, 1200, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel leftPanel = new JPanel();
        frame.getContentPane().add(leftPanel, BorderLayout.WEST);
        leftPanel.setLayout(new GridLayout(0, 1, 0, 0));

        JPanel searchPanel = new JPanel();
        leftPanel.add(searchPanel);
        searchPanel.setLayout(new GridLayout(2, 0, 0, 0));

        JPanel panel_3 = new JPanel();
        searchPanel.add(panel_3);

        JButton btnMainPage = new JButton("主页");
        btnMainPage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            Store window = new Store();
                            window.frame.setVisible(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
        panel_3.add(btnMainPage);


        JPanel panel_2 = new JPanel();
        searchPanel.add(panel_2);

        JLabel lblNewLabel = new JLabel("search");
        panel_2.add(lblNewLabel);

        textField = new JTextField();
        panel_2.add(textField);
        textField.setColumns(10);

        JButton btnSearch = new JButton("\u641C\u7D22");

        panel_2.add(btnSearch);

        JList gameList = new JList();
        leftPanel.add(gameList);

        JPanel bottomPanel = new JPanel();
        frame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
        bottomPanel.setLayout(new BorderLayout(0, 0));

        JButton btnAdd = new JButton("\u6DFB\u52A0");

        bottomPanel.add(btnAdd, BorderLayout.WEST);

        JButton btnChatting = new JButton("\u804A\u5929");
        bottomPanel.add(btnChatting, BorderLayout.EAST);

        JPanel botCenPanel = new JPanel();
        bottomPanel.add(botCenPanel, BorderLayout.CENTER);

        Component horizontalGlue_1 = Box.createHorizontalGlue();
        botCenPanel.add(horizontalGlue_1);

        JButton btnDownload = new JButton("\u4E0B\u8F7D");

        botCenPanel.add(btnDownload);

        Component horizontalGlue = Box.createHorizontalGlue();
        botCenPanel.add(horizontalGlue);

        JPanel centerPanel = new JPanel();
        frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
        centerPanel.setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        centerPanel.add(panel, BorderLayout.NORTH);

        JLabel lblImages = new JLabel("images");
        panel.add(lblImages);

        JPanel panel_1 = new JPanel();
        centerPanel.add(panel_1, BorderLayout.CENTER);
        panel_1.setLayout(new BorderLayout(0, 0));

        JLabel lblName = new JLabel("name");
        panel_1.add(lblName, BorderLayout.NORTH);

        JPanel panel_4 = new JPanel();
        panel_1.add(panel_4, BorderLayout.CENTER);
        panel_4.setLayout(new GridLayout(0, 1, 0, 0));

        JScrollPane scrollPane = new JScrollPane();
        panel_4.add(scrollPane);

        JTextArea textArea_information = new JTextArea();
        textArea_information.setText("\u6587\u5B57\u63CF\u8FF0");
        textArea_information.setLineWrap(true);
        textArea_information.setWrapStyleWord(true);
        textArea_information.setEditable(false);
        scrollPane.setViewportView(textArea_information);

        JPanel panel_5 = new JPanel();
        panel_4.add(panel_5);
        panel_5.setLayout(new BorderLayout(0, 0));

        JLabel label = new JLabel("\u95EE\u9898\u53CD\u9988");
        panel_5.add(label, BorderLayout.NORTH);

        JPanel panel_6 = new JPanel();
        panel_5.add(panel_6, BorderLayout.CENTER);
        panel_6.setLayout(new BorderLayout(0, 0));

        JPanel panel_7 = new JPanel();
        panel_6.add(panel_7, BorderLayout.SOUTH);
        panel_7.setLayout(new BorderLayout(0, 0));

        JButton btnSubmit = new JButton("\u63D0\u4EA4");
        btnSubmit.setEnabled(false);

        panel_7.add(btnSubmit, BorderLayout.EAST);

        JPanel panel_8 = new JPanel();
        panel_6.add(panel_8, BorderLayout.CENTER);
        panel_8.setLayout(new GridLayout(0, 1, 0, 0));

        JPanel panel_9 = new JPanel();
        panel_8.add(panel_9);
        panel_9.setLayout(new BorderLayout(0, 0));

        JLabel label_1 = new JLabel("\u95EE\u9898\u7C7B\u578B\uFF1A");
        panel_9.add(label_1, BorderLayout.WEST);

        JPanel panel_11 = new JPanel();
        panel_9.add(panel_11, BorderLayout.CENTER);
        panel_11.setLayout(new BorderLayout(0, 0));

        JComboBox comboBox = new JComboBox();
        comboBox.setEnabled(false);
        panel_11.add(comboBox, BorderLayout.WEST);


        Component verticalStrut = Box.createVerticalStrut(20);
        panel_11.add(verticalStrut, BorderLayout.SOUTH);

        Component verticalStrut_1 = Box.createVerticalStrut(20);
        panel_11.add(verticalStrut_1, BorderLayout.NORTH);


        JPanel panel_10 = new JPanel();
        panel_8.add(panel_10);
        panel_10.setLayout(new BorderLayout(0, 0));

        JLabel label_2 = new JLabel("\u8BE6\u7EC6\u63CF\u8FF0\uFF1A");
        panel_10.add(label_2, BorderLayout.NORTH);

        JTextArea textArea_problem = new JTextArea();
        textArea_problem.setEditable(false);
        panel_10.add(textArea_problem, BorderLayout.CENTER);

        final Timer[] timer = new Timer[game.length];

        updateJList(gameList);
        updateJComboBox(comboBox);
        frame.addWindowFocusListener(new WindowFocusListener() {
            public void windowGainedFocus(WindowEvent arg0) {
                //textField.requestFocusInWindow();
            }

            public void windowLostFocus(WindowEvent arg0) {
            }
        });

//        textField.addKeyListener(new java.awt.event.KeyAdapter() {
//
//            public void keyPressed(KeyEvent e) {
//
//                int k = e.getKeyCode();
//
//                if (k == '\n') {
//                    btnSearch.addActionListener(new ActionListener() {
//                        public void actionPerformed(ActionEvent e) {
//                            JOptionPane.showMessageDialog(null,"搜索成功");
//                        }
//                    });
//                }
//
//            }
//
//        });

        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int j = comboBox.getSelectedIndex();
                if (j != -1) {
                    textArea_problem.requestFocusInWindow();
                }
            }
        });
        btnMainPage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnDownload.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = comboBox.getSelectedItem().toString();
                String msg = textArea_problem.getText();
                if ("".equals(msg) || msg == null) {
                    JOptionPane.showMessageDialog(null, "请输入问题详细描述！");
                    textArea_problem.requestFocusInWindow();
                } else {
                    JOptionPane.showMessageDialog(null, "提交成功，请等候回复！");
                }
                textArea_problem.setText("");
            }
        });

        //list点击事件
        gameList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                int j = gameList.getSelectedIndex();

                //显示图片名字描述
                if (j == -1) {
                    textArea_information.setText("");
                    lblName.setText("name");
                    lblImages.setText("images");
                    btnSubmit.setEnabled(false);
                    comboBox.setEnabled(false);
                    textArea_problem.setEditable(false);
                } else {
                    String[] string;
                    switch (j) {
                        case 0:
                            string = pubg_image;
                            break;
                        case 1:
                            string = nba_2k_image;
                            break;
                        case 2:
                            string = pico_image;
                            break;
                        case 3:
                            string = yongJie_image;
                            break;
                        default:
                            string = pubg_image;
                    }
                    //设置图片
                    for (int i = 0; i < game.length; i++) {
                        if (i != j && timer[i] != null && timer[i].isRunning())
                            timer[i].stop();
                    }
                    ImageIcon imageIcon = new ImageIcon(string[0]);
                    lblImages.setIcon(imageIcon);
                    final int[] i = {1};
                    timer[j] = new Timer(2500, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            if (i[0] > 2) {
                                i[0] = 0;
                            } else if (i[0] < 0) {
                                i[0] = string.length - 1;
                            }
                            ImageIcon imageIcon = new ImageIcon(string[i[0]]);
                            lblImages.setIcon(imageIcon);
                            i[0]++;
                        }
                    });
                    timer[j].start();

                    lblImages.setText("");
                    btnSubmit.setEnabled(true);
                    comboBox.setEnabled(true);
                    textArea_problem.setEditable(true);
                    textArea_problem.requestFocusInWindow();
                    lblName.setText(game[j]);
                    textArea_information.setText(game_information[j]);
                }
            }
        });

    }

    //更新combobox下拉框
    public static void updateJComboBox(JComboBox comboBox) {
        DefaultComboBoxModel dcmd = new DefaultComboBoxModel(game_problem);
        comboBox.setModel(dcmd);
    }

    //更新gameList
    public void updateJList(JList list) {
        DefaultListModel<String> model = new DefaultListModel<>();
        model.setSize(game.length);
        for (int i = 0; i < game.length; i++) {
            model.set(i, game[i]);
        }
        list.setModel(model);
    }
}


